  <?php
include APPLICATION_PATH."/models/base/AgentBase.php";
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class Model_Agent extends Model_AgentBase{
    
}
class Model_AgentMapper extends Model_AgentMapperBase{
     public static function findbyid($user_id, Model_Agent $entry){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select= 'select * from agent where user_id='.$user_id.'';
        $stmt=$db->query($select);
        $row = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        if($row==NULL){
            return NULL;
        } else {
        $entry->setId($row[0]->id)
                    ->setId($row[0]->id)
                    ->setName($row[0]->name)
                    ->setAddress($row[0]->address)
                    ->setProvince_Id($row[0]->province_id)
                    ->setDistrict_Id($row[0]->district_id)
                    ->setAgent_Cat_Id($row[0]->agent_cat_id)
                    ->setPhone($row[0]->phone)
                    ->setRepresentavie($row[0]->representavie)
                    ->setUser_Id($row[0]->user_id)
                    ->setArea_Id($row[0]->area_id)
                    ->setCharges($row[0]->charges)
                    ->setCreated($row[0]->created)
                    ->setCreated_By($row[0]->created_by)
                    ->setModified($row[0]->modified)
                    ->setModified_By($row[0]->modified_by)
                    ->setOrder($row[0]->order)
                    ->setStatus($row[0]->status)
                    ->setAgent_Status($row[0]->agent_status)
                    ->setTax_Code($row[0]->tax_code)
                    ->setIdentity_Card($row[0]->identity_card)
                    ->setBirthday($row[0]->birthday)
                    ->setAbility_Sell($row[0]->ability_sell)
                    ->setLegal_Capital($row[0]->legal_capital)
                    ->setPerson($row[0]->person)
                    ->setAbility_Archive($row[0]->ability_archive);
        }
    }
    
    public static function getlastid(){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select= 'select max(id) as id from agent';
        $stmt=$db->query($select);
        $row = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        return $row[0]->id;        
    }
    
    public static function getChargesBuy($user_id){
        $db=  Zend_Db_Table::getDefaultAdapter();
        $select= "select ROUND(SUM(`order_detail`.charges),2) as charges from `agent`,`order`,`order_detail` where `agent`.user_id=".$user_id.
                " and `order`.store_id=`agent`.id and `order`.order_no=`order_detail`.order_id and `order_detail`.order_status_id=2";
        $stmt=$db->query($select);
        $row=$stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        return $row[0]->charges;
    }
    public static function getChargesSell($user_id){
        $db=  Zend_Db_Table::getDefaultAdapter();
        $select= "select ROUND(SUM(`order_detail`.charges),2) as charges from `agent`,`order_detail` where `agent`.user_id=".$user_id.
                " and `order_detail`.agent_id=`agent`.id and `order_detail`.order_status_id=2";
        $stmt=$db->query($select);
        $row=$stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        return $row[0]->charges;
    }
    //Tất cả:                                   $agent_cat_id=0
    //Đại lý cấp 1:                             $agent_cat_id=1
    //Cửa hàng và tất cả không thuộc DL cấp 1:  $agent_cat_id=2
    public static function getAgent($agent_cat_id=0){
        $db=  Zend_Db_Table::getDefaultAdapter();
        $where = " 1=1";
        if($agent_cat_id==1) $where = " agent_cat_id=1";
        if($agent_cat_id==2) $where = " agent_cat_id!=1";
        $select = "Select agent.id as agent_id,agent.name as agent_name, 
ROUND(IFNULL(agent.charges,0),2) as agent_charges,IFNULL(agent.charges_sell,0) as agent_charges_sell,
IFNULL(agent.user_id,0) as agent_user_id,agent.agent_status as agent_status,IFNULL(province.name,'') as province_name, IFNULL(district.name,'') as district_name
From agent left join province on agent.province_id = province.id left join district on agent.district_id = district.id
WHERE ".$where."
Group By agent.id";
        $stmt=$db->query($select);
        $rows=$stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row){
            $array= array(
                'agent_id'=>$row->agent_id,
                'agent_name'=>$row->agent_name,
                'agent_status'=>$row->agent_status,
                'user_id'=>$row->agent_user_id,
                'charges'=>$row->agent_charges,
                'charges_sell'=>$row->agent_charges_sell,
                'district_name'=>$row->district_name,
                'province_name'=>$row->province_name
            );
            array_push($entries, $array);
        }
        return $entries;
    }
    
    public static function getAgentByAdminId($admin_id){
        $db=Zend_Db_Table::getDefaultAdapter();
        $select="Select agent.id as agent_id,agent.name as agent_name,IFNULL(agent.address,'') as agent_address,IFNULL(province.name,0) as province_name,IFNULL(district.name,0) as district_name,IFNULL(agent_category.name,0) as agent_cat_name 
                From agent left join agent_category on agent.agent_cat_id= agent_category.id 
				left join district on agent.district_id= district.id
				left join province on agent.province_id = province.id
                where agent.province_id in (Select admin_area.area_id from admin left join admin_area on admin.id= admin_area.admin_id where admin.id='".$admin_id."')";
        $stmt=$db->query($select);
        $rows=$stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row){
            $array= array(
                'agent_id'=>$row->agent_id,
                'agent_name'=>$row->agent_name,
                'province_name'=>$row->province_name,
                'district_name'=>$row->district_name,
                'address'=>$row->agent_address,
                'agent_cat_name'=>$row->agent_cat_name                
            );
            array_push($entries, $array);
        }
        return $entries; 
        
    }
    
    public static function checkAgentName($agent_name){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select= "select max(id) as id from agent where name='$agent_name'";
        $stmt=$db->query($select);
        $row = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        if($row==NULL){
            return 1;
        } else {
            return $row[0]->id;
        }
    }
    public static function findbyagentid($agent_id, Model_Agent $entry){
        $db = Zend_Db_Table::getDefaultAdapter();
        $select= 'select * from agent where id='.$agent_id.'';
        $stmt=$db->query($select);
        $row = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        if($row==NULL){
            return NULL;
        } else {
        $entry->setId($row[0]->id)
                    ->setId($row[0]->id)
                    ->setName($row[0]->name)
                    ->setAddress($row[0]->address)
                    ->setProvince_Id($row[0]->province_id)
                    ->setDistrict_Id($row[0]->district_id)
                    ->setAgent_Cat_Id($row[0]->agent_cat_id)
                    ->setPhone($row[0]->phone)
                    ->setRepresentavie($row[0]->representavie)
                    ->setUser_Id($row[0]->user_id)
                    ->setArea_Id($row[0]->area_id)
                    ->setCharges($row[0]->charges)
                    ->setCreated($row[0]->created)
                    ->setCreated_By($row[0]->created_by)
                    ->setModified($row[0]->modified)
                    ->setModified_By($row[0]->modified_by)
                    ->setOrder($row[0]->order)
                    ->setStatus($row[0]->status)
                    ->setAgent_Status($row[0]->agent_status)
                    ->setTax_Code($row[0]->tax_code)
                    ->setIdentity_Card($row[0]->identity_card)
                    ->setBirthday($row[0]->birthday)
                    ->setAbility_Sell($row[0]->ability_sell)
                    ->setLegal_Capital($row[0]->legal_capital)
                    ->setPerson($row[0]->person)
                    ->setAbility_Archive($row[0]->ability_archive);
        return $entry;
        }
    }
}

